import {
  queryLayoutInAuth, list, queryTypes,
  queryAbility, queryCreateTenant, refreshCache,
  deleteByIds, change, exportXml, exportTemplate,
  queryAbilityByTypeCode, queryApis, queryRenderRule,
  querySupportParent, create, queryById, queryPermListInAuth,
  listPageInnerMapInAuth
} from '@/api/menu'
import { addRouters } from '@/router'
import { convertMenuToRouter, setCacheRouter, getCacheInnerMenu, setCacheInnerMenu } from '@/utils/menu'

const getDefaultState = () => {
  return {
  }
}

const state = getDefaultState()

const mutations = {

}

const actions = {
  getLayoutInAuth() {
    return new Promise(function (resolve, reject) {
      queryLayoutInAuth().then(res => {
        if (res.code === 0) {
          // 组装vue菜单
          try {
            const routers = convertMenuToRouter(res.data);
            // 添加路由
            addRouters(routers);
            // 缓存路由
            setCacheRouter(routers);
            // 缓存页面内菜单（按钮）
            listPageInnerMapInAuth().then(res => {
              if (res.code === 0) {
                setCacheInnerMenu(res.data);
              }
            })
          } catch (e) {
            addRouters([]);
          }

        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 查询菜单列表
   * @param {object} searParams 检索参数
   */
  list({ commit }, searParams) {
    return new Promise(function (resolve, reject) {
      list(searParams).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 查询菜单类型
   */
  getTypes() {
    return new Promise(function (resolve, reject) {
      queryTypes().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 查询菜单能力
   */
  getAbility() {
    return new Promise(function (resolve, reject) {
      queryAbility().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 通过菜单类型编码获取菜单能力
   */
  getAbilityByTypeCode({ commit }, typeCode) {
    return new Promise(function (resolve, reject) {
      queryAbilityByTypeCode(typeCode).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 查询菜单创建人
   */
  getCreateTenant() {
    return new Promise(function (resolve, reject) {
      queryCreateTenant().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 刷新菜单
   */
  refreshCache() {
    return new Promise(function (resolve, reject) {
      refreshCache().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 删除菜单
   * @param {*} menuIds 菜单id 
   */
  removeByIds({ commit }, menuIds) {
    return new Promise(function (resolve, reject) {
      deleteByIds(menuIds).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 修改菜单
   * @param {*} menu 菜单数据
   */
  modify({ commit }, menu) {
    return new Promise(function (resolve, reject) {
      change(menu).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 导出菜单
   * @param {array} menuIds 菜单id
   */
  exportXml({ commit }, menuIds) {
    return new Promise(function (resolve, reject) {
      exportXml(menuIds).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 导出菜单模板
   */
  exportTemplate({ commit }) {
    return new Promise(function (resolve, reject) {
      exportTemplate().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 查询接口信息
   */
  getApis() {
    return new Promise(function (resolve, reject) {
      queryApis().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 通过菜单类型查询渲染规则
   */
  getRenderRule({ commit }, typeCode) {
    return new Promise(function (resolve, reject) {
      queryRenderRule(typeCode).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 通过菜单类型查询支持的父菜单
   */
  getSupportParent({ commit }, typeCode) {
    return new Promise(function (resolve, reject) {
      querySupportParent(typeCode).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 创建菜单
   */
  create({ commit }, data) {
    return new Promise(function (resolve, reject) {
      create(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 通过id查询菜单
   */
  getById({ commit }, data) {
    return new Promise(function (resolve, reject) {
      queryById(data).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  /**
   * 查询当前租户权限列表菜单
   */
  getPermListInAuth() {
    return new Promise(function (resolve, reject) {
      queryPermListInAuth().then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

